<style lang="scss" scoped>
    .marquee {
        padding: 10px 0;
        position: relative;
        overflow: hidden;
        border-radius: 3px;
        background: #fff;
        box-shadow: 0 1px 3px rgba(0, 0, 0, .1);

        &::before, &::after {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 20px;
            z-index: 2;
            background: -webkit-linear-gradient(right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 80%, rgba(255, 255, 255, 1) 100%);
        }

        &::after {
            left: inherit !important;
            right: 0 !important;
            background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 80%, rgba(255, 255, 255, 1) 100%);
        }
    }

    .marquee-wrapper {
        width: 100000px;

        &:hover {
            animation-play-state: paused !important;
        }

        > .cont {
            float: left;
            width: auto;

            > .item {
                display: inline-block;
                margin-right: 20px;

                a:hover {
                    box-shadow: 0 0 2px rgba(#f90, 1);
                    border-radius: 3px;
                    background: rgba(#f90, .1);
                }

                .title {
                    color: #555;
                    max-width: 200px;
                    display: block;
                    padding-right: 5px;
                }

                .time {
                    font-size: 12px !important;
                    color: #555;
                    display: block;
                    width: 140%;
                    padding-left: 14px;
                    text-align: left;
                    overflow: hidden;
                    margin-left: -12px;
                    transform: scale(calc(11 / 12));
                    transform-origin: 0 0;
                }

                .icon {
                    margin: 3px 5px 0;
                    background: #ccc;
                    color: #fff;
                    width: 30px;
                    height: 30px;
                    border-radius: 50%;
                    line-height: 30px;
                    @keyframes bolt {
                        from {
                            color: #fff;
                        }
                        to {
                            color: #F56C6C;
                        }
                    }

                    &.warning {
                        background: #E6A23C;
                    }

                    &.success {
                        background: #67C23A;
                    }

                    &.danger {
                        background: #F56C6C;
                        animation: bolt 1s linear 0s infinite;
                    }

                    &.primary {
                        background: #409EFF;
                    }
                }
            }

        }
    }
</style>
<template>
    <div ref="marquee" class="marquee" :style="{width:`${width}`}">
        <div class="marquee-wrapper animation-paused" ref="wrapper" :style="styleWrapper">
            <div class="cont" v-for="i in twice" :key="i">
                <span @click="callback(item.id)" class="item" v-for="(item, index) in data" :key="index">
                    <router-link :to="item.state" v-if="!scopedSlots && item.state" class="ui flex">
                        <i class="icon" :class="iconClass(item.type)"></i>
                        <span class="item one">
                            <span class="title ui-ellipsis">{{ +index+1 }}/ {{ item.title }}</span>
                            <span class="time">{{item.sub}}</span>
                        </span>
                    </router-link>
                    <a href="javascript:" v-if="!scopedSlots && !item.state" class="ui flex">
                        <i class="icon" :class="iconClass(item.type)"></i>
                        <span class="item one">
                            <span class="title ui-ellipsis">{{ +index+1 }}/ {{ item.title }}</span>
                            <span class="time">{{item.sub}}</span>
                        </span>
                    </a>
                    <slot :items="{item,index}"></slot>
                </span>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        name: 'ui-marquee',
        props: {
            data: {
                type: Array,
                required: true
            },
            width: {
                type: String,
                default: '100%'
            }
        },
        data() {
            return {
                keyframes: 'marqueeAnimation' + parseInt(Math.random() * 10000),
                time: '',
                styleSheet: '',
                scopedSlots: false,
                twice: 2
            }
        },
        mounted() {

            this.scopedSlots = (typeof this.$scopedSlots.default === 'function') //判断插槽是否存在

            this.$nextTick(() => {
                // console.dir(this.$refs.marquee.clientWidth)

                let divWidth = this.$refs.wrapper.childNodes[0].clientWidth; //动态获取容器长度
                // console.log(divWidth)

                if (divWidth < this.$refs.marquee.clientWidth) {
                    this.twice = 1
                    return
                }

                this.time = (+divWidth / 80) // 依据div长度设置跑马灯滚动的快慢 (默认速率为80px/s)

                let style = document.createElement('style')  //创建一个style元素
                let head = document.getElementsByTagName('head')[0]; //获取head元素

                // 生成keyframes样式表
                let textNode = document.createTextNode(`@keyframes ${ this.keyframes }{100%{margin-left:-${ divWidth }px}}`)

                // 把样式插入dom
                style.id = this.keyframes
                style.appendChild(textNode)
                head.appendChild(style)

            })

        },
        computed: {
            // 在标签上插入动画style
            styleWrapper() {
                return {
                    animation: `${ this.keyframes } ${ this.time }s linear 0s infinite`
                }
            }
        },
        methods: {
            iconClass(val) {
                switch(val){
                    case 'danger' :
                        return 'danger bolt'
                    case 'success' :
                        return 'success check'
                    case 'primary' :
                        return 'primary flag'
                    case 'warning' :
                        return 'warning'
                    default:
                        return 'comment alternate'
                }
            },
            callback(id) {
                this.$emit('callback', id)
            }
        },
        beforeDestroy() {
            //删除动态插入的样式
            let sheet = document.getElementById(this.keyframes)
            sheet.parentNode.removeChild(sheet)

        }
    }
</script>
